// index page
// ============

define(['require', 'zepto', 'utils', 'wx', 'ejs', 'qrcode', 'sui'], function(require, $, utils, wx, ejs, QRCodeJs) {
  return {
    init: function(data) {
      this.courseId = data.courseId;
      this.unionid=data.unionid
      this.verificate();
      utils.configWeixin(data.jsConfig, false);
      this.requestData(data);
      this.headerHtml = '<a href="javascript:void(0);" class="item-content shopping-content">\
          <% if(product.id != 1) {%>\
          <div class="item-media" style="<%= product.single_avatar == true ? "width: 6rem; display: block;" : "" %>">\
            <% if(product.single_avatar == false) {%>\
            <img src="'+window.__CTX__.resource+'<%=product.coach_avatar%>.jpg?v=1.1.1" style="width: 6rem;">\
            <% } else { %>\
             <% product.coach_avatar_list.forEach(function(imageItem){%>\
               <img src="'+window.__CTX__.resource+'<%=imageItem%>.jpg?v=1.1.1" style="width: 6rem; margin-bottom: .3rem;">\
             <% }) %>\
             <% } %>\
          </div>\
          <% } %>\
      <div class="item-inner <%=product.id == 1 ? "no-pic" : "" %>">\
      <div class="item-title-row">\
      <div class="item-title"><%=product.name%></div>\
      </div>\
      <div class="item-subtitle">\
      <div class="">课程导师：\
      <% product.coach_name_list.forEach(function(name, index){%>\
      <%= index == 0 ? "" : "&" %> <%=name%>\
      <% }) %>\
      </div>\
      <div class="">上课时间：<%=product.occur_time%></div>\
      </div>\
      <div class="item-text" style="height: 3rem;">\
        <div><%=product.id == 1 ? "精英" : "" %>装备包</div>\
        <div><%=product.equipment_package%></div>\
      </div>\
    </div>\
  </a>\
</div>\
<div class="black-block"></div>\
<div class="details-layout">\
  <p>姓名：<%=userInfo.name%></p>\
  <p>手机号：<%=user.mobile%></p>\
  <p>性别：<%=userInfo.gender%></p>\
  <p>年龄：<%=userInfo.age%></p>\
  <br>\
  <p>短袖尺码：<%=order.clothes_size%></p>\
  <% if(product.id == 1) {%>\
    <p>鞋子尺码：<%=userInfo.shoe_size%></p>\
  <% } %>\
  <br>\
  <p>订单编号：<%=order.code%></p>\
</div>\
</div>\
</div>';
      
    },
    
    requestData: function(data) {
      var that = this;
      $.ajax({
        type : 'GET',
        url : window.__CTX__.domain + '/api/profile/courses/' + data.courseId + "?unionid=" + that.unionid,
        dataType : 'json',
        timeout : 30000,
        success : function(res) {
          console.log(res);
          if(res.code == 0) {
              that.product = res.data.product
              that.order = res.data.order
              that.user = res.data.user,
              that.userInfo = res.data.userInfo
              var headerResult = ejs.render(that.headerHtml, {product : that.product, order: that.order, user: that.user, userInfo: that.userInfo})
              $('.product-info').html(headerResult);
          } else {
            $.toast("获取我的课程数据失败");
          } 
        },
        error : function(xhr, type) {
          alert('Ajax error!')
        }
      })
    },
    
    verificate: function() {
      var that = this;
      
      $(document).on('click', '#verificate', function(e) {
        e.preventDefault()
        
        $.confirm('确认核销吗？', function () {
          
          var codes = new Array();
          $('input[name="codes"]:checked').each(function() {
            codes.push($(this).val());
          });
          
          $.ajax({
            type : 'POST',
            url : window.__CTX__.domain + '/api/staffs/verificate',
            data: {
              'courseId': that.courseId,
              'codes': codes.join(',')
            },
            success : function(res) {
              console.log(res);
              if(res.code == 0) {
                $.toast("核销成功");
                window.location.reload();
              } else {
                $.toast("核销失败");
              } 
            },
            error : function(xhr, type) {
              alert('Ajax error!')
            }
          })
          
        });
        
      });
    }
    
  }
})